// pages/goodsDetail/goodsDetail.js
import httpUtil from '../../utils/httpUtil.js'
var url = 'https://domopluses.com'
var token = wx.getStorageSync("token")
var phone = wx.getStorageSync("phone")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    imgUrls: [],
    swiperIndex: 0, //这里不写第一次启动展示的时候会有问
    num: 1, //数量
    goodid:'',
    goodname: '',
    oldprice: 0, //原价
    nowprice: 0, //现价
    contents: '',
    total_price: '', //总价
    goodid: '',
  },
  bindchange(e) {
    this.setData({
      swiperIndex: e.detail.current
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that=this;
    that.setData({
      goodid: options.goodid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  swiperChange(e) {
    this.setData({
      swiperIndex: e.detail.current
    })
  },
  //获取页面信息
  getLunbo: function() {
    var that = this;
    var req = {
      id: that.data.goodid
    }
    httpUtil.post("/crm/getgood", req)
      .then(function(res) {
        if (res.data.status == 200) {
          var img = [];
          for (var i = 0; i < res.data.data.imglist.length; i++) {
            img.push(res.data.data.imglist[i])
          }
          that.setData({
            imgUrls: img,
            contents: res.data.data.contents,
            oldprice: res.data.data.oldprice,
            nowprice: res.data.data.nowprice,
            goodname: res.data.data.goodname,
            goodid: res.data.data.id
          })
          that.post()

        }
      }).catch(function(res) {
        console.log(res);
      });
  },
  //获取优惠券信息
  post: function () {
    var that = this;
    if (wx.getStorageSync("userId") == '' || wx.getStorageSync("userId") == undefined) {
      wx.navigateTo({
        url: '/pages/login/login'
      })
    }
    var req = {
      id: that.data.goodid,
      userid: wx.getStorageSync('userId')
    }
    httpUtil.post("/crm/getcoupon", req)
      .then(function (res) {
        if (res.data.status == 200) {
          var arr = [];
          for (var i = 0; i < res.data.data.length; i++) {
            arr.push(res.data.data[i])
          }

          that.setData({
            list: arr,
          })
        }
      }).catch(function (res) {
        console.log(res);
      });
  },
  // 领取优惠券
  receive: function (e) {
    var that = this;
    var req = {
      couponid: e.currentTarget.dataset.id,
      userid: wx.getStorageSync('userId')
    }
    httpUtil.post("/crm/usergetcoupon", req)
      .then(function (res) {
        if (res.data.status == 200) {
          that.post();
          wx.showToast({
            icon: 'none',
            duration: 2000,
            title: '' + res.data.message + '',
          })
        }
      }).catch(function (res) {
        console.log(res);
      });
  },

  add: function() {
    var that = this;
    that.data.num++
      this.setData({
        num: that.data.num,
        total_price: that.data.nowprice * that.data.num,
      })
  },
  reduce: function() {
    if (this.data.num <= 1) {
      return;
    }
    this.data.num -= 1;
    this.setData({
      num: this.data.num,
      total_price: this.data.nowprice * this.data.num,
    })
  },
  //购买
  buy: function() {
    wx.navigateTo({
      url: '/pages/goodsDetail1/goodsDetail1'
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    //获取当前网络状态
    wx.getNetworkType({
      success: function(res) {
        // 返回网络类型, 有效值：
        // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
        if (res.networkType == 'none' || res.networkType == 'unknown') {
          that.setData({
            outline: true,
          })
        } else {
          that.setData({
            outline: false,
          })
          that.getToken()
        }
      }
    })

    //监听网络状态
    wx.onNetworkStatusChange(function(res) {
      if (res.networkType == 'none' || res.networkType == 'unknown') {
        that.setData({
          outline: true
        })
      } else {
        that.setData({
          outline: false,
        })
        that.getToken()
      }
    })
  },
  // 获取token
  getToken: function() {
    var that = this;
    wx.login({
      success: function(res) {
        if (res.code) {
          var req = JSON.stringify({
            code: res.code
          })
          //获取sessionKey
          wx.request({
            url: 'https://www.tiantong369.com/gettoken',
            data: req,
            method: "POST",
            //头信息设置
            header: {},
            success: function(res) {
              if (res.data.status == 200) {
                wx.setStorageSync("token", res.data.token);
                that.getLunbo()
              }
            },
            fail: function(res) {

            }
          });
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }

})